﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using FileUpload.DAL;
using FileUpload.DAL.Entities;
using FileUpload.Models;

namespace FileUpload.Controllers
{
    public class ImagesController : Controller
    {
        private FileUploadContext db = new FileUploadContext();
        // GET: Images/Create
        public ActionResult Create(Guid token)
        {
            ViewBag.token = token;
            return View();
        }

        // POST: Images/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Id")] Image image, HttpPostedFileBase photo, Guid  token)
        {
            if (ModelState.IsValid)
            {
                image.Id = Guid.NewGuid();
                image.Token = token;
                
                if (photo != null)
                {
                    byte[] imageByte = new byte[photo.ContentLength];
                    photo.InputStream.Read(imageByte, 0, photo.ContentLength);
                    image.Photo = imageByte;
                    image.TimeStamp = DateTime.Now;
                }
                db.Images.Add(image);
                await db.SaveChangesAsync();
                //return RedirectToAction("Index");
            }

            return View(image);
        }
        [HttpPost]
        public async Task<ActionResult> GetImage(string token)
        {
            string  url="";
            var img = db.Images.Where(i => i.Token.ToString() == token).FirstOrDefault();

            if (img != null)
            {
                 url = Convert.ToBase64String(img.Photo);
                db.Images.Remove(img);
                await db.SaveChangesAsync();
                return Json(new JsonResultModel { Status = (int)JsonResultStatus.Success, Message = "获取成功", Data = url });
            }
            return Json(new JsonResultModel { Status = (int)JsonResultStatus.Error, Message = "没有数据", Data = null });

        }

        //// GET: Images/Delete/5
        //public async Task<ActionResult> Delete(Guid? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Image image = await db.Images.FindAsync(id);
        //    if (image == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(image);
        //}

        //// POST: Images/Delete/5
        //[HttpPost, ActionName("Delete")]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> DeleteConfirmed(Guid id)
        //{
        //    Image image = await db.Images.FindAsync(id);
        //    db.Images.Remove(image);
        //    await db.SaveChangesAsync();
        //    return RedirectToAction("Index");
        //}

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
